// 导入数据库操作模块
const { mysql } = require("../../db");
const moment = require("moment");
const { filesURL } = require("../../../config");

const formatData = (results) => {
	try {
		const data = results.map(o => ({
			...o,
			cover: o.cover ? `${filesURL}/images/cover/${o.cover}` : null,
			country:
				o.country === 0 ? "中国" :
					o.country === 1 ? "日本" :
						o.country === 2 ? "欧美" : "其他",
			op: o.op ? `${filesURL}/videos/${o.op}` : null,
			ed: o.ed ? `${filesURL}/videos/${o.ed}` : null,
			premiere: moment(o.premiere).format("YYYY年MM月DD日"),
			issue_time: moment(o.issue_time).format("YYYY年MM月DD日"),
			address: `https://pan.baidu.com/s/${o.address}`,
			create: moment(o.create).format("YYYY-MM-DD HH:mm:ss"),
			delete: moment(o.delete).format("YYYY-MM-DD HH:mm:ss"),
		}));
		return data;
	} catch (err) {
		throw err;
	}
};

exports.Info = async (req, res, table) => {
	try {
		const sqlStr = `
            SELECT g.*, userinfo.nickname AS creator
            FROM ${table} AS g
            JOIN userinfo ON g.creator = userinfo.id
            WHERE g.is_delete = 0;
        `;
		const [results] = await mysql.query(sqlStr);
		if (results.length === 0) return res.cc("暂无Galgame!");
		const data = formatData(results);
		return res.success(data);
	} catch (err) {
		return res.error(err);
	}
};

exports.Add = async (req, res, table) => {
	const { number, galname, c_name, country, intro, premiere, issue_time, address, pwd } = req.body;
	const info = { number, galname, c_name, country, intro, premiere, issue_time, address, pwd, creator: req.user.id };

	try {
		const sqlStr = `INSERT INTO ${table} SET ?`;
		const [results] = await mysql.query(sqlStr, [info]);
		if (results.affectedRows !== 1) return res.error("新增失败！");
		return res.success("新增成功！");
	} catch (err) {
		return res.error(err);
	}
};

exports.Update = async (req, res, table) => {
	const { number, galname, nickname, country, intro, premiere, issue_time, address, pwd, id } = req.body;
	const info = [number, galname, nickname, country, intro, premiere, issue_time, address, pwd, id];

	try {
		const sqlStr = `UPDATE ${table} SET number=?,galname=?,nickname=?,country=?,intro=?,premiere=?,issue_time=?,address=?,pwd=? WHERE id = ?`;
		const [results] = await mysql.query(sqlStr, [info]);
		if (results.affectedRows !== 1) return res.error("更新失败！");
		return res.success("更新成功！", 0);
	} catch (err) {
		return res.error(err);
	}
};

exports.Search = async (req, res, table) => {
	const info = req.params.value;

	try {
		const sqlStr = `
        SELECT g.*, u.nickname AS creator
        FROM ${table} AS g
        JOIN userinfo u ON g.creator = u.id
        WHERE (g.galname LIKE ? OR g.c_name LIKE ? OR u.nickname LIKE ? OR g.intro LIKE ? OR g.rate LIKE ? OR g.country LIKE ? OR g.number LIKE ?) AND g.is_delete = 0;    
    `;

		const SeletePattern = `%${info}%`;
		const values = Array(7).fill(SeletePattern);
		const [results] = await mysql.query(sqlStr, values);
		if (results.length === 0) return res.error("该Galgame不存在！");
		const data = formatData(results);
		return res.success(data);
	} catch (err) {
		return res.error(err);
	}
};